﻿using Padsurface.Audio.Engine.Interfaces;
using System;
using System.Threading;
using System.Threading.Tasks;

namespace Padsurface.Audio.Engine
{
    internal class MetronomeStoppedState : IMetronomeState
    {
        public bool CanStart
        {
            get { return false; }
        }

        public Task<bool> Start(Metronome context)
        {
            if (context == null) throw new ArgumentNullException("context");

            context.SetState(new MetronomeStartingState());

            context.StartCompletionTask = new TaskCompletionSource<bool>();
            context.CancellationTokenSource = new CancellationTokenSource();

            Task.Factory.StartNew(context.Tick, context.CancellationTokenSource.Token);

            return context.StartCompletionTask.Task;
        }

        public Task<bool> Stop(Metronome context)
        {
            throw new InvalidOperationException();
        }

        public void Reset(Metronome context)
        {
            throw new InvalidOperationException();
        }
    }
}